-- hookable.lua
-- hook interface

Hookable = class()

function Hookable:register_hook(name)
    local hookname = name..'_hooks';
    self[hookname] = {};
    trace('register hook '..hookname);
end

-- register event hook
function Hookable:hook_event(name, func, receiver)
    local hookname = name..'_hooks';
    local hooks = self[hookname];
    if nil == hooks then
        logger:error('there is no hook_event named '..name);
    else
        table.insert(self[hookname], {func, receiver});
    end
end

-- invoke hooked events
function Hookable:invoke_hooks(name, ...)
    local hookname = name..'_hooks';
    local hooks = self[hookname];
    if nil ~= hooks then
        for _, v in ipairs(hooks) do
            local func = v[1];
            local obj = v[2];
            func(obj, ...);
        end
    end
end
